from django.db import models
from django.db.models import Index


class ImUser(models.Model):
    username = models.CharField(max_length=50, unique=True)
    password = models.CharField(max_length=255)
    is_online = models.BooleanField(default=False)
    avatar = models.ImageField(null=True)
    class Meta:
        db_table = "pyim_user"
        indexes = [Index(fields=["username"])]

class ImMessage(models.Model):
    sender = models.ForeignKey(ImUser, related_name="sender", on_delete=models.CASCADE)
    receiver = models.ForeignKey(ImUser, related_name="receiver", on_delete=models.CASCADE)
    time = models.DateTimeField(auto_now=True)
    is_read = models.BooleanField(default=False)
    message = models.TextField()
    class Meta:
        db_table = "pyim_message"
        indexes = [Index(fields=['sender', 'receiver']),
                   Index(fields=['sender']),
                   Index(fields=['receiver']),
                   Index(fields=['receiver', 'is_read'])]